unit uniGroundMetal;

interface

uses
  uniGround,
  gl,
  Classes;

type
  { GroundMetal }

  GroundMetal = class(Ground)
  public
    size: Single;

    constructor Create;
    destructor Destroy; override;

    procedure drawGL;  override;
    procedure setVariation(Index: Integer);  override;

    procedure loadTextures;  override;
    procedure deleteTextures;  override;
  end;


implementation

uses
  PNGLoader,
  uniConfig,
  uniGlobal,
  uniGroundMetalSegment;

{ GroundMetal }

constructor GroundMetal.Create;
var
  s: array[0..1] of Single;
  seg, tmp: GroundSegment;
begin
  inherited Create;

  pos[0] := 0.0;
  pos[1] := 0.0;
  pos[2] := 0.0;

  variation := 0;
  loadTextures;

  size := 21.0;
  s[0] := size;
  s[1] := size;
  rootSeg := GroundMetalSegment.Create(pos, s, Self);

  pos[1] := size * 2.0;
  seg := GroundMetalSegment.Create(pos, s, Self);
  rootSeg.next := seg;

  pos[1] := 0.0;
  tmp := GroundMetalSegment.Create(pos, s, Self);
  seg.next := tmp;
  tmp.back := seg;
end;

procedure GroundMetal.deleteTextures;
begin
  glDeleteTextures(1, @tex[Integer(Base)]);
  glDeleteTextures(1, @tex[Integer(Blip)]);
  tex[Integer(Base)] := 0;
  tex[Integer(Blip)] := 0;
end;

destructor GroundMetal.Destroy;
begin
  deleteTextures;
  //XXX ADD clear method
  rootSeg.next.next.Destroy;
  rootSeg.next.Destroy;
  rootSeg.Destroy;
  //inherited;
end;

procedure GroundMetal.drawGL;
var
  seg: GroundSegment;
  tmp: GroundSegment;
  s2: Single;
  pulse: Single;
  game: Global;

  p: array[0..2] of Single;
  s: array[0..1] of Single;
begin
  s2 := size * 2.0;
  //-- Set background color for low and med gfx
  pulse := sin(uniGlobal.gameFrame * 0.03);
  if pulse < 0.0 then pulse := 0.0;
  glClearColor(0.2 + pulse, 0.2, 0.25, 1.0);

  //-- draw ground segments
  game := getInstance;
  if (not game.game_pause) or (uniGlobal._gameMode = Menu) then
  begin
    seg := rootSeg.next;
    while Boolean(seg) do
    begin
      seg.pos[1] := seg.pos[1] + uniGlobal.scrollSpeed * uniGlobal.speedAdj;
      seg := seg.next;
    end;
  end;

  seg := rootSeg.next;
  while Boolean(seg) do
  begin
    seg.drawGL;

    if seg.pos[1] < -s2 then
    begin
      p[0] := 0.0;
      p[1] := seg.pos[1] + s2 + s2;
      p[2] := 0.0;

      s[0] := size;
      s[1] := size;

      //seg.back.next := 0;
      //seg.Destroy;
      tmp := GroundMetalSegment.Create(p, s, Self);
      rootSeg.next.back := tmp;
      tmp.next := rootSeg.next;
      tmp.back := rootSeg;
      rootSeg.next := tmp;
      Break;
    end;
    seg := seg.next;
  end;
end;

procedure GroundMetal.loadTextures;
var
  _config: Config;
  tbc: array[0..3] of Single;
begin
  _config := uniConfig.instance; 
  tbc[0] := 0.2;
  tbc[1] := 0.2;
  tbc[2] := 0.2;
  tbc[3] := 1.0;

  case variation of
    0: LoadTexture('png/gndMetalBase00.png', tex[Integer(Base)]);
    1: LoadTexture('png/gndMetalBase01.png', tex[Integer(Base)]);
    2: LoadTexture('png/gndMetalBase02.png', tex[Integer(Base)]);
  else
    LoadTexture('png/gndMetalBase00.png', tex[Integer(Base)]);
  end;

  if _config.texBorder then glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, @tbc);
  LoadTexture('png/gndMetalBlip.png', tex[Integer(Blip)]);
end;

procedure GroundMetal.setVariation(Index: Integer);
begin
  variation := index mod 3;
  deleteTextures;
  loadTextures;
end;

end.

